package com.bjpowernode.other.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 公平锁
 */
public class Lock03 {

    public static ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) {
        new Thread(() -> {m1();},"t1").start();
        new Thread(() -> {m1();},"t2").start();
        new Thread(() -> {m1();},"t3").start();
        new Thread(() -> {m1();},"t4").start();
        new Thread(() -> {m1();},"t5").start();
    }

    public static void m1() {
        for (int i = 0; i < 2 ; i++) {
            try {
                lock.lock();
                Thread.sleep(2);
                System.out.println(Thread.currentThread().getName());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }
        }
    }
}
